﻿using Dapper;
using PDFDesign.Dapper;
using System;
using System.Collections.Generic;
using System.Data;

namespace PDFDesign.Repository
{
    public interface IRepository<T> : IDisposable where T : class
    {
        //select
        T GetById(dynamic primaryId);
        T GetById(IDbConnection conn, dynamic primaryId, IDbTransaction trans);
        T GetDefaultByName(string colName, string value);
        T GetFirst(string sql, dynamic param = null, bool buffered = true);
        T GetFirst(IDbConnection conn, string sql, dynamic param = null, IDbTransaction trans = null, bool buffered = true);
        IEnumerable<T> GetByIds(IList<dynamic> ids, string name);
        IEnumerable<T> GetAll();
        IEnumerable<T> Query(string sql, dynamic param = null, bool buffered = true);
        IEnumerable<T> Query(IDbConnection conn, string sql, dynamic param = null, IDbTransaction trans = null, bool buffered = true);

        //count
        int Count(IDbConnection conn, IPredicate predicate);
        int Count(IDbConnection conn, string sql);
        int Count(string sql, dynamic parameters = null);

        //lsit
        IEnumerable<T> GetList(IDbConnection conn, IPredicate predicate = null,
            IList<ISort> sort = null, bool buffered = false);


        //paged select
        IEnumerable<T> GetPaged(IDbConnection conn, int pageIndex, int pageSize, object predictate,
            IList<ISort> sort = null, bool buffered = false);

        //execute
        Int32 Execute(string sql, dynamic param = null);
        Int32 Execute(IDbConnection conn, string sql, dynamic param = null, IDbTransaction transaction = null);
        Int32 ExecuteCommand(IDbCommand cmd);
        Int32 ExecuteProc(string procName, DynamicParameters param = null);
        Int32 ExecuteProc(IDbConnection conn, string procName, DynamicParameters param = null);
        IList<T> ExecProcQuery(string procName, DynamicParameters param);
        IList<T> ExecProcQuery(IDbConnection conn, string procName, DynamicParameters param);

        //insert, update, delete
        dynamic Insert(T entity);
        dynamic Insert(IDbConnection conn, T entity, IDbTransaction transaction = null);
        bool Update(T entity);
        bool Update(IDbConnection conn, T entity, IDbTransaction transaction = null);
        bool UpdateBatch(IDbConnection conn, IEnumerable<T> entityList, IDbTransaction transaction = null);
        bool Delete(dynamic primaryId);
        bool Delete(IDbConnection conn, dynamic primaryId, IDbTransaction transaction = null);
        bool Delete(IDbConnection conn, IPredicate predicate, IDbTransaction transaction = null);
        int DeleteBatch(IDbConnection conn, string name, IEnumerable<dynamic> ids);
    }
}
